<?php

// +----------------------------------------------------------------------

// | Description: 客户

// +----------------------------------------------------------------------

// | Author: zl

// +----------------------------------------------------------------------



namespace app\crm\controller;



use app\admin\controller\ApiCommon;

use app\crm\model\BalanceLog;

use app\crm\model\Setting;

use app\crm\model\SettingCategory;

use app\crm\model\Service;

use app\crm\model\ReturnVist;

use app\crm\model\ReturnVistT;

use app\crm\model\Customer as CustomerModel;

use app\crm\model\TransferShop;

use app\crm\model\CustomerConfig as CustomerConfigModel;

use app\crm\model\CustomerGrade as CustomerGradeModel;

use think\response\Json;

use think\facade\Db;

use think\facade\Request;

use app\admin\model\Coupon as CouponModel;

use app\common\traits\ActionRecordTrait;

use app\shop\model\unionpay\AppConfig;
use app\shop\model\unionpay\AppUtil;
use app\shop\model\unionpay\Test;


class CustomerInventory extends ApiCommon

{
    protected $customerModel;

    protected $customerConfigModel;

    /**

     * 用于判断权限

     * @permission 无限制

     * @allow 登录用户可访问

     * @other 其他根据系统设置

     **/

    public function initialize()

    {

        $action = [

            'permission' => ['shopcustomerlist'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth',$action);

        $request = $this->request;

        $a = strtolower($request->action());



        $this->customerModel = new CustomerModel();

        $this->customerConfigModel = new CustomerConfigModel();

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

    
	public function shopCustomerList(){
		
		$startTime  = $_GET['start_time'];
		$endTime  = $_GET['end_time'];
		$sTime = strtotime('2022-01-01');
		$start_time_int = strtotime($startTime);
		$end_time_int = strtotime($endTime)+86399;
		
		$where  = [];
		
		array_push($where,['type','=',1]);
		
		array_push($where,['shop_id','<>',74076159037951]);
		
		//array_push($where,['shop_type','=',2]);//
		array_push($where,['status','=',1]);//
		
		$shopList = Db::name('shop')->where($where)->field('shop_id,shop_name,market_name,shop_type')->select()->toArray();
		/* echo '<pre>';
		print_r($shopList);exit; */
		
		foreach ($shopList as $k => $v) {
			
			//会员数据分类
			//$memberListY = $this->membershipList($v['shop_id'],$start_time_int,$end_time_int,$sTime);
			/* echo '<pre>';
			print_r($memberListY);//exit */
			//新单数据统计
			//$singleListY = $this->shopSingleList($v['shop_id'],$v['shop_type'],$start_time_int,$end_time_int,$sTime);
			
			//补单数据统计
			$scarpoxListBH = $this->shopScarpoxList('BH',$v['shop_id'],$v['shop_type'],$start_time_int,$end_time_int);
			
			$scarpoxListXM = $this->shopScarpoxList('XM',$v['shop_id'],$v['shop_type'],$start_time_int,$end_time_int);
			
			//流失预警 -4  流失顾客是3 
			/* $alertListY = $this->shopLiushiList('3',$v['shop_id']);
			$wastersListY = $this->shopLiushiList('4',$v['shop_id']); */
			
			$list[$k]['shop_name'] = $v['shop_name'];
			$list[$k]['holderNum'] = '';
			$list[$k]['arrivalNum'] = '';
			$list[$k]['membership'] = 0;
			$list[$k]['cjNum'] = '65%';
			$list[$k]['caseNum'] =0;
			$list[$k]['wastersNum'] = 0;
			$list[$k]['alertNum'] = 0;
			$list[$k]['magnolia'] = 0;
			$list[$k]['purpleMember'] = 0;
			$list[$k]['goldMember'] = 0;
			$list[$k]['SecondaryScarpoxNum'] = '8';
			$list[$k]['penetrationNum'] = '8';
			$list[$k]['singleNum'] = 0;
			$list[$k]['singleMoney'] = 0;
			$list[$k]['scarpoxNum'] = $scarpoxListBH['budanmoney'];
			$list[$k]['projectNum'] =$scarpoxListXM['budanmoney'];
		} 
		
		/* echo '<pre>';
		print_r($list);exit; */
		//导出报表--表头设置 
		$callName = [
			
			'shop_name' =>'门店',
		    'holderNum' =>'保有人数',
		    'arrivalNum' =>'到店率',
		    'membership' =>'入会率',
		    'cjNum' =>'成交保有率',
		    'caseNum' =>'优秀案例',
			'wastersNum' =>'流失人数',
			'alertNum' =>'预警人数',
			'magnolia' =>'白玉兰会员',
			'purpleMember' =>'紫玉兰会员',
			'goldMember' =>'金玉兰会员',
			'SecondaryScarpoxNum' =>'疤痘二次开发率',
			'penetrationNum' =>'项目普及率',
			'singleNum' =>'新单个数',
			'singleMoney' =>'新单金额',
			'scarpoxNum' =>'疤痘补单',
			'projectNum' =>'项目补单',
			
		
		];
		
		$downUrl = exportExcelData('客户盘点分析报表',$callName,$list,'xlsx',1);
		
		echo "<script language='javascript'>";
		
		echo " location='".$downUrl."';";
		
		echo "</script>";
	}
	
	//入会率 白玉兰会员数 紫玉兰会员数 金玉兰会员数
	public function  membershipList($shopId,$startTime,$endTime,$sTime){
		
		// 新增会员顾客数
		$xz_huiyuan_list = Db::name('customer')->where([['shop_id', '=', $shopId], ['join_time', 'between', "$startTime,$endTime"]])->field('count(id) as sum,shop_id')->select()->toArray();
		
		// 达到条件顾客数
		$cj_list = Db::name('customer')->where([['shop_id', '=', $shopId], ['member_classification', 'in', [71,72,73]],['membership_leve','=',0],['final_consumption','between',"$startTime,$endTime"], ['consumption', '>=', '5000'], ['create_time','>=',$sTime]])->field('count(id) as sum,shop_id')->select()->toArray();
		
		// 白玉兰会员数
		$xzw_huiyuan_list = Db::name('customer')->where([['shop_id', '=', $shopId], ['join_time', 'between', "$startTime,$endTime"], ['membership_leve', '=', "1"]])->field('count(id) as sum,shop_id')->select()->toArray();
		
		// 紫玉兰会员数
		$xzp_huiyuan_list = Db::name('customer')->where([['shop_id', '=', $shopId], ['join_time', 'between', "$startTime,$endTime"], ['membership_leve', '=', "2"]])->field('count(id) as sum,shop_id')->select()->toArray();
		
		// 金玉兰会员数
		$xzg_huiyuan_list = Db::name('customer')->where([['shop_id', '=', $shopId], ['join_time', 'between', "$startTime,$endTime"], ['membership_leve', '=', "3"]])->field('count(id) as sum,shop_id')->select()->toArray();
		
		//优案人数
		/* $case_list = Db::name('case')->field('count(id) as sum,shop_id')->where([['shop_id', '=', $shopId], ['tg_excellent_time', 'between', "$startTime,$endTime"], ['selection_results', 'in', [2,3]]])->select()->toArray();
		print_r($case_list);exit; */
		
		//$field = ['id,shop_id,customer_id,nurse_id,position_name,reason_name,type_name,create_time,selection_results,sp_excellent_time,tg_excellent_time,collection_time,reviewer_id,is_collection,collection_time,case_results,telephone_selection','num','sqr_id'];
		//$list = Db::name('case')->field($field)->where($where)->limit(($request['page'] - 1) * $request['limit'], $request['limit'])->order('id DESC')->select()->toArray();
		// print_r($where);exit;
		$where = [];
		array_push($where,['shop_id','=',$shopId]);
		array_push($where,['tg_excellent_time','>=',$startTime]);
		array_push($where,['tg_excellent_time','<=',$endTime]);
		array_push($where,['selection_results','in',[2,3]]);
		
		$dataCount = Db::name('case')->where($where)->count();
		/* echo '<pre>';
		print_r($dataCount); exit; */
		
		$data['ruhuilv'] = $cj_list[0]['sum']==0? '0%' :bcmul(bcdiv($xz_huiyuan_list[0]['sum'],$cj_list[0]['sum'],10),100,2).'%'; 
		$data['baiyulan'] = !empty($xzw_huiyuan_list) ? $xzw_huiyuan_list[0]['sum'] : '0';
		$data['ziyulan'] = !empty($xzp_huiyuan_list) ? $xzp_huiyuan_list[0]['sum'] : '0';
		$data['jinyulan'] = !empty($xzg_huiyuan_list) ? $xzg_huiyuan_list[0]['sum'] : '0'; 
		$data['caseNum'] = $dataCount;
		
		return $data;
	}
	
	//新单个数 新单金额 疤痘补单 项目补单
	public function shopSingleList($shopId,$shopType,$startTime,$endTime,$sTime){
		
		//新单个数 
		$receive_deal=Db::name('customer')->where([['shop_id', '=', $shopId], ['cj_time', 'between', "$startTime,$endTime"],['cj_money', '>=', 500]])->field('count(DISTINCT(id)) as sum,shop_id')->select()->toArray();
		
		$where = [];
		$where['order_type']=1;
		$wheret['r.order_type']=1;
		//收款单中的疤痕业绩 不含店转的业绩--01
		$receive = Db::name('shop_refund_order')->alias('r')
			
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		    
		    //->field('sum(g.pay_price * r.coefficient) as pay_amount')
			->field('sum(g.pay_price) as pay_amount')
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where($wheret)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',3)
		        
		    ->where('r.status','=',2)
			
		    ->where('r.refund_type','=','101.02')
			
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
			
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
			
		        return $query->whereTime('deal_time', '<=', $endTime);
			
		    },function ($query) use ($endTime) {
			
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    ->find();
			
		    // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
			
			
		//退款单中的疤痕业绩	--不含店转的业绩  --02
		$refund = Db::name('shop_refund_order')->alias('r')
			
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
			
		    //->field('sum(g.pay_price * r.coefficient) as pay_amount')
			->field('sum(g.pay_price) as pay_amount')
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where($wheret)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',1)
		        
		    ->where('r.status','=',2)
			
		    ->where('r.refund_type','=','101.02')
			
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
			
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
			
		        return $query->whereTime('deal_time', '<=', $endTime);
			
		    },function ($query) use ($endTime) {
			
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    ->find();
			
		    // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
			
			
		//门诊转店到 门店的 通过门店的归属ID 查询	--收款单 --03
		$receive_zd =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount,id')
			
			->where($where)
			
		    ->where('referral_shop_id','=',$shopId)
			
		    ->where('type','=',3)
		
		    ->where('status','=',2)
		
		    ->where('refund_type','=','101.02')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();	
			
			
		//退款单是店转的 疤痕/项目业绩查询  --04
		$refund_dz = Db::name('shop_refund_order')->alias('r')
			
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
			
		    ->field('sum(g.pay_price * r.coefficient) as pay_amount')
			
		    ->where($wheret)
			
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','=','06')
		    
		    ->where('r.type','=',1)
		        
		    ->where('r.status','=',2)
			
		    ->where('r.refund_type','<>','101.03')
			
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
			
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
			
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
			
		        return $query->whereTime('deal_time', '<=', $endTime);
			
		    },function ($query) use ($endTime) {
			
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    ->find();
			
		    // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
			
			
		//收款单是店转的 疤痕/项目业绩---05
		$receive_dz = Db::name('shop_refund_order')->alias('r')
			
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
			
		    ->field('sum(g.pay_price * r.coefficient) as pay_amount')
		
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where($wheret)
		    
		    ->where('g.refund_mode','=','06')
		    
		    ->where('r.type','=',3)
		        
		    ->where('r.status','=',2)
			
		    ->where('r.refund_type','=','101.02')
			
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
			
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
			
		        return $query->whereTime('deal_time', '<=', $endTime);
			
		    },function ($query) use ($endTime) {
			
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    ->find();
			
		    // ->column('r.pay_amount * r.coefficient as pay_amount','r.id');
			
			
		//门诊收款单 是非疗程的 疤痕/项目业绩  --06
		$receive_mz =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount')
			
			->where($where)
			
		    ->where('shop_id','=',$shopId) 
			
			->where('is_treatment','=',2)
		    
		    ->where('type','=',3) 
			
			->where('referral_shop_id','>',0)
			
		    ->where('status','=',2)
		
		    ->where('refund_type','<>','101.03')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();
		
		if($shopType==1){ //1是门诊 2是门店
		
			$xindanMoney =  round(($receive['pay_amount']-$refund['pay_amount'] +$receive_dz['pay_amount']-$receive_mz['pay_amount']-$refund_dz['pay_amount']),2);
		}else{
			//实收金额 - 退款金额 + 门诊收钱分给店面的一半金额  
			$xindanMoney =  round(($receive['pay_amount']-$refund['pay_amount'] +$receive_zd['pay_amount']+$receive_dz['pay_amount']-$refund_dz['pay_amount']),2);
		}
		
		$data['xindanNum'] = $receive_deal[0]['sum'];  //新单个数
		$data['xindanMoney'] =$xindanMoney ;
		
		return $data;
	}
	//疤痕补单 和项目补单的数据 
	public function shopScarpoxList($scene,$shopId,$shopType,$startTime,$endTime){
		
		switch ($scene) {
			case 'BH':
				
		        $where['order_type']=2;
				$wheret['r.order_type']=2;
				
				$where['business_type']=1;
				$wheret['r.business_type']=1;
				break;
		
		    case 'XM':
				
				$where['business_type']=2;
				$wheret['r.business_type']=2;
				
		        $where['order_type']=2;
				$wheret['r.order_type']=2;
				break;
		
		    default:
		
		        break;
		
		}
		//收款单中的疤痕业绩 不含店转的业绩--01 
		$receive = Db::name('shop_refund_order')->alias('r')
			
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
		    
		    //->field('sum(g.pay_price * r.coefficient) as pay_amount')
			->field('sum(g.pay_price) as pay_amount')
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where($wheret)
		    
		    ->where('g.refund_mode','<>','06')
		    
			//->where('r.order_type','=',2)
			
		    ->where('r.type','=',3)
		        
		    ->where('r.status','=',2)
			
		    ->where('r.refund_type','=','101.02')
			
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
			
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
			
		        return $query->whereTime('deal_time', '<=', $endTime);
			
		    },function ($query) use ($endTime) {
			
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    ->find();
			
		    
		//退款单中的疤痕业绩	--不含店转的业绩  --02
		$refund = Db::name('shop_refund_order')->alias('r')
			
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
			
		    //->field('sum(g.pay_price * r.coefficient) as pay_amount')
			->field('sum(g.pay_price) as pay_amount')
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where($wheret)
		    
		    ->where('g.refund_mode','<>','06')
		    
		    ->where('r.type','=',1)
		        
		    ->where('r.status','=',2)
			
		    ->where('r.refund_type','=','101.02')
			
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
			
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
			
		        return $query->whereTime('deal_time', '<=', $endTime);
			
		    },function ($query) use ($endTime) {
			
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    ->find();
			
		//门诊转店到 门店的 通过门店的归属ID 查询	--收款单 --03
		$receive_zd =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount,id')
			
			->where($where)
			
		    ->where('referral_shop_id','=',$shopId)
			
		    ->where('type','=',3)
		
		    ->where('status','=',2)
		
		    ->where('refund_type','=','101.02')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();	
			
			
		//退款单是店转的 疤痕/项目业绩查询  --04
		$refund_dz = Db::name('shop_refund_order')->alias('r')
			
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
			
		    ->field('sum(g.pay_price * r.coefficient) as pay_amount')
			
		    ->where($wheret)
			
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where('g.refund_mode','=','06')
		    
		    ->where('r.type','=',1)
		        
		    ->where('r.status','=',2)
			
		    ->where('r.refund_type','<>','101.03')
			
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
			
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
			
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
			
		        return $query->whereTime('deal_time', '<=', $endTime);
			
		    },function ($query) use ($endTime) {
			
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    ->find();
			
		    
		//收款单是店转的 疤痕/项目业绩---05
		$receive_dz = Db::name('shop_refund_order')->alias('r')
			
		    ->join('shop_refund_order_goods g', 'r.id=g.order_id', 'LEFT')
			
		    ->field('sum(g.pay_price * r.coefficient) as pay_amount')
		
		    ->where('r.shop_id','=',$shopId)
		    
		    ->where($wheret)
		    
		    ->where('g.refund_mode','=','06')
		    
		    ->where('r.type','=',3)
		        
		    ->where('r.status','=',2)
			
		    ->where('r.refund_type','=','101.02')
			
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
			
		        return $query->whereTime('deal_time', '>=', $startTime);
		            
		    })
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
			
		        return $query->whereTime('deal_time', '<=', $endTime);
			
		    },function ($query) use ($endTime) {
			
		        return $query->whereMonth('deal_time');
		    
		    })
		    
		    ->find();
			
		    
		//门诊收款单 是非疗程的 疤痕/项目业绩  --06
		$receive_mz =Db::name('shop_refund_order')
		
		    ->field('sum(pay_amount * coefficient) as pay_amount')
			
			->where($where)
			
		    ->where('shop_id','=',$shopId) 
			
			->where('is_treatment','=',2)
		    
		    ->where('type','=',3) 
			
			->where('referral_shop_id','>',0)
			
		    ->where('status','=',2)
		
		    ->where('refund_type','<>','101.03')
		
		    ->when($startTime ?? 0, function ($query) use ($startTime) {
		
		        return $query->whereTime('deal_time', '>=', $startTime);
		
		    })
		
		    ->when($endTime ?? 0, function ($query) use ($endTime) {
		
		        return $query->whereTime('deal_time', '<=', $endTime);
		
		    },function ($query) use ($endTime) {
		
		        return $query->whereMonth('deal_time');
		
		    })->find();
		
		if($shopType==1){ //1是门诊 2是门店
			//return round($receive['pay_amount']-$refund['pay_amount'] +$receive_dz['pay_amount']+bcsub(0,$receive_mz['pay_amount'],2)+bcsub(0,$refund_dz['pay_amount']),2);
			$budanAmount =  round(($receive['pay_amount']-$refund['pay_amount'] +$receive_dz['pay_amount']-$receive_mz['pay_amount']-$refund_dz['pay_amount']),2);
		}else{
			//实收金额 - 退款金额 + 门诊收钱分给店面的一半金额  
			$budanAmount =  round(($receive['pay_amount']-$refund['pay_amount'] +$receive_zd['pay_amount']+$receive_dz['pay_amount']-$refund_dz['pay_amount']),2);
		}
		
		$data['budanmoney']=$budanAmount ;
		
		return $data;
	}
	
	//门店优案人数  门店流失人数 门店 预警人数 
	public function shopLiushiList($is_join_shop,$shopId){
		
		$qichu_time = strtotime('2022-01-01 00:00:00');
		      
		// 流失预警-4 流失顾客-3
		if($is_join_shop==4){
			$time1 = strtotime('-1 month');
			$time2 = strtotime('-4 month');
		}

		if($is_join_shop==3){
			$time1 = strtotime('-4 month');
			$time2 = '0';
		}
		 
		// 优案且已经毕业的顾客
		$yx_where = [];
		// $my_where = [];

		array_push($yx_where,['graduation_type','=','1']);
		array_push($yx_where,['selection_results','in',[2,3]]);

		$yx_customer_list = Db::name('case')->where($yx_where)->column('distinct customer_id');

		// echo "<pre>";
		// print_r($yx_customer_list);
		// exit;

		$client_list = [];

		foreach ($yx_customer_list as $k => $v) {
			$where = [];

			array_push($where,['customer_id','=',$v]);

			$caseList = Db::name('case')->where($where)->column('id,customer_id,selection_results,tg_excellent_time,graduation_type,position_id,position_name');

			$anli_num = count($caseList);
			
			if($anli_num<2){
				array_push($client_list,$v);
			}else{
				$position_list=[];// 部位
				$max_time = 0;// 最新评选时间
				$graduation_type = 0;// 毕业状态
				$selection_results = 0;// 评选结果
				foreach ($caseList as $k1 => $v1) {
					if(in_array($v1['position_id'],$position_list)){
						// 该部位已经评选为优秀或者满意案例了
					}else{
						if($v1['selection_results']=='2' || $v1['selection_results']=='3'){
							if($v1['graduation_type']=='1'){
								// 优秀或者满意 且已经毕业
								array_push($position_list,$v1['position_id']);
							}else{
								if($v1['tg_excellent_time']>$max_time){
									$max_time = $v1['tg_excellent_time'];
									$graduation_type = $v1['graduation_type'];
									$selection_results = $v1['selection_results'];
								}
							}
						}else{
							if($v1['tg_excellent_time']>$max_time){
								$max_time = $v1['tg_excellent_time'];
								$graduation_type = $v1['graduation_type'];
								$selection_results = $v1['selection_results'];
							}
						}
					}
				}

				if($max_time>$time1){
					array_push($client_list,$v);
				}

			}

		}
		
		// 排除已经评选上优案且已经毕业的顾客
		$where = [];
		
		array_push($where,['id','not in',$client_list]);
		array_push($where,['cj_time','>',$qichu_time]);
		array_push($where,['final_consumption','<',$time1]);
		array_push($where,['final_service','<',$time1]);

	   
		array_push($where,['shop_id','=',$shopId]);
		        
		$shopIdmz=[73782775131388,73782774881835,73782775392774,73782775377728,73782775206631,73782775085647];

		array_push($where,['shop_id','not in',$shopIdmz]);

		array_push($where,['member_label','not like','%588%']);

		array_push($where,['member_label','not like','%589%']);
				
		$dataCounts   = Db::name('customer')
			->where($where)
			->where(function ($query) use ($time1,$time2) {
				$cj_where1 = [];
				$cj_where2 = [];
				
				array_push($cj_where1,['final_consumption','>',$time2]);
				array_push($cj_where1,['final_consumption','<',$time1]);
			
				array_push($cj_where2,['final_service','>',$time2]);
				array_push($cj_where2,['final_service','<',$time1]);
			
			
				$query->where(function ($query) use ($cj_where1){
					$query->where($cj_where1);
				});
				$query->whereOr(function ($query) use ($cj_where2){
					$query->where($cj_where2);
				});
				
			})
			->count(); 
		
		$data['customerNum'] = $dataCounts;
		
		return $data;
	}
}